<html>
	<head>
		<title>The Real Franker!</title>
		<script type="text/javascript" src="http://www.google.com/jsapi?key=INSERT-YOUR-KEY">
		</script>
		<script type="text/javascript">
			google.load("language", "1");
			var bingAppID = "INSERT-YOUR-KEY";
		</script>
		<script type="text/javascript">
			if (!localStorage["engine"]) { localStorage["engine"] = "GOOGLE" };
			if (!localStorage["langSrc"]) { localStorage["langSrc"] = "auto" };
			if (!localStorage["langDst"]) { localStorage["langDst"] = "en" };
			if (!localStorage["shortcutFrankate"]) { localStorage["shortcutFrankate"] = "Ctrl+Alt+F" };
			if (!localStorage["shortcutClean"]) { localStorage["shortcutClean"] = "Ctrl+Alt+C" };
			if (!localStorage["style"]) { localStorage["style"] = "color: #292;" };
			if (!localStorage["inject_before"]) { localStorage["inject_before"] = "false" };
			if (!localStorage["inject_brackets"]) { localStorage["inject_brackets"] = "true" };
	    </script>
		<script type="text/javascript">
			var frankerPort;
			function translate(text) {
				var langSrc = localStorage["langSrc"];
				if (langSrc == "auto") {
					langSrc = "";
				}
				var langDst = localStorage["langDst"];
				var engine = localStorage["engine"];
				
				if (engine == "GOOGLE") {
					google.language.translate(text, langSrc, langDst, translateCallBack);
				} else if (engine == "BING") {
					var bingURL = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=translateCallBack&appId="+bingAppID+"&from=" + langSrc + "&to=" + langDst + "&contentType=text/html&text=" + encodeURIComponent(text);
					injectScript("frankerBingScript", bingURL);
				}
				
			}
			function translateCallBack(result) {
				var translation = "";
				var engine = localStorage["engine"];
				if (engine == "GOOGLE") {
					translation = result.translation;
				} else if (engine == "BING") {
					translation = result;
				}
				frankerPort.postMessage({name:"frankateSelectionResponse", message:translation});
			}
			function respondToMessage(theMessageEvent) {
		    	if(theMessageEvent.name === "frankateSelectionRequest") {
					translate(theMessageEvent.message);
				} else if (theMessageEvent.name === "shortcutFrankateSelectionRequest") {
					frankerPort.postMessage({name:"shortcutFrankateSelectionValue", message:localStorage["shortcutFrankate"]});
				} else if (theMessageEvent.name === "shortcutFrankateCleanRequest") {
					frankerPort.postMessage({name:"shortcutFrankateCleanValue", message:localStorage["shortcutClean"]});
				} else if (theMessageEvent.name === "styleDestinationRequest") {
					frankerPort.postMessage({name:"styleDestinationValue", message:localStorage["style"]});
				} else if (theMessageEvent.name === "injectBeforeRequest") {
					frankerPort.postMessage({name:"injectBeforeValue", message:localStorage["inject_before"]});
				} else if (theMessageEvent.name === "injectBracketsRequest") {
					frankerPort.postMessage({name:"injectBracketsValue", message:localStorage["inject_brackets"]});
				}
			}
			function injectScript(id, url){
				var s = document.getElementById(id);
				if (s != null) {
					document.getElementsByTagName("head")[0].removeChild(s);
				}
				s = document.createElement("script");
				s.id = id;
				s.type = "text/javascript";
				s.src = url;
				document.getElementsByTagName("head")[0].appendChild(s);
			}
			
			chrome.extension.onConnect.addListener(function(port) {
			  frankerPort = port;
			  console.assert(port.name == "Franker");
			  port.onMessage.addListener(respondToMessage);
			});
		</script>
		<script type="text/javascript">
			function frankerContextOnClick(info, tab) {
				chrome.tabs.executeScript(tab.id,{code:"frankerInjectFrankate();",allFrames:true});
			}
			chrome.contextMenus.create({"title": "Frankate", "contexts":["selection"],
                                       "onclick": frankerContextOnClick});
		</script>
	</head>
	<body>
	</body>
</html>
